﻿using System;
using System.Collections.Generic;
using System.Data;

using System.IO;
using System.Reflection;
using System.Timers;

using XmlBander.Common.Enum;
using XMK.Utils;
using System.Threading;

namespace XMK.Tadong.InnerElement
{
    public class Tasker
    {
        static Dictionary<string, Thread> _taskDict = new Dictionary<string, Thread>();
        static DataTable _timersTable = null;
        internal static DataTable TimersTable
        {
            get
            {
                if (_timersTable == null)
                {
                    EnumHelper enumHelper = new EnumHelper(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Timers.xml"));
                    enumHelper.GetEnum("Timers");
                    _timersTable = enumHelper.EnumItems;
                }
                return _timersTable;
            }
        }

        public static void StartAll()
        {
            foreach (DataRow dr in TimersTable.Rows)
            {
                try
                {
                    if (!Convert.ToBoolean(dr["Available"]))
                    {
                        continue;
                    }
                    string _class = dr["Class"].ToString();
                    object assem = Assembly.Load(_class.Substring(0, _class.LastIndexOf('.'))).CreateInstance(_class);
                    if (assem == null)
                    {
                        continue;
                    }
                    if (!_taskDict.ContainsKey(_class))
                    {
                        ITask task = (assem as ITask);
                        task.SleepMillSeconds = ParseHelper.Parse<int>(dr["Interval"]);
                        task.RowCount = ParseHelper.Parse<int>(dr["RowCount"]);
                        Thread datasyncThread = new Thread(()=>
                        {
                            task.Task();
                        });
                        _taskDict.Add(dr["TimerEnName"].ToString(), datasyncThread);
                        datasyncThread.IsBackground = true;
                        datasyncThread.Start();
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLog(LogType.Error, "[Tasker.Start:]" + ex.ToString());
                }
            }
        }

        public static void Start(string taskName)
        {
            if (!_taskDict.ContainsKey(taskName))
            {
                Console.WriteLine(string.Concat(taskName, "不存在"));
            }
            else
            {
                Thread t = _taskDict[taskName];
                if (t.ThreadState == ThreadState.Running)
                {
                    Console.WriteLine(taskName + "正在运行!");
                }
                else
                {
                    Console.WriteLine("该操作暂不支持，等待后续开发");
                }
                
            }
        }
    }
}
